package com.jqb.hibernatebbs.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jqb.hibernatebbs.pojo.Bbsuser;
import com.jqb.hibernatebbs.service.BbsUserService;

public class UserServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		if (name == null || password == null){
			request.getRequestDispatcher("/WEB-INF/error.jsp").forward(request, response);
		}else{
			Bbsuser user = new Bbsuser();
			user.setName(name);
			user.setPassword(password);
			user = BbsUserService.newInstance().checkUser(user);
			//验证成功转到显示所有帖子的页面
			if(user!=null){
				request.getSession().setAttribute("user", user);
				request.getRequestDispatcher("/msgServlet?opt=show").forward(request, response);
			}else{
				request.getRequestDispatcher("/WEB-INF/error.jsp").forward(request, response);
			}
		}
	}

}
